﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.Configuration;

namespace Kis2012.Models
{
    public class UserRolesTable
    {
        private Table<UserRoles> userRolesTable;
        private DataContext context;

        public UserRolesTable()
        {
            string connStr = ConfigurationManager.ConnectionStrings["DBConn"].ConnectionString;
            context = new DataContext(connStr);
            userRolesTable = context.GetTable<UserRoles>();
        }

        public IEnumerable<UserRoles> GetRolesByName(string userName, string appName)
        {
            //UserRoles userRoles = userRolesTable.SingleOrDefault(u => u.UserName == userName && u.AppName == appName);
            return userRolesTable.Where(u => u.UserName == userName && u.AppName == appName).AsEnumerable();
        }

        public IEnumerable<UserRoles> GetUsersByRole(string roleName, string appName)
        {
            //UserRoles userRoles = userRolesTable.SingleOrDefault(u => u.RoleName == roleName && u.AppName == appName);
            return userRolesTable.Where(u => u.RoleName == roleName && u.AppName == appName).AsEnumerable();

            //return GetAllRoles().SelectMany(u => u.UserName).AsEnumerable<UserRoles>();
        }

        public IEnumerable<UserRoles> GetAllRoles()
        {
            return userRolesTable.AsEnumerable();
        }

        public bool IsUserInRole(string userName, string roleName, string appName)
        {
            UserRoles userRoles = userRolesTable.SingleOrDefault(u => u.UserName == userName && u.RoleName == roleName && u.AppName == appName);
            return (userRoles != null);
        }

        public int RegisterUserRole(string userName, string userRole, string appName)
        {
            UserRoles userRoles = new UserRoles();
            userRoles.UserName = userName;
            userRoles.RoleName = userRole;
            userRoles.AppName = appName;
            userRolesTable.InsertOnSubmit(userRoles);
            context.SubmitChanges();
            
            return userRoles.UserRoleId;
        }

        public bool DeleteRole(string userName, string roleName, string appName)
        {
            UserRoles userRoles = userRolesTable.SingleOrDefault(u => u.UserName == userName && u.RoleName == roleName && u.AppName == appName);
            if (userRoles != null)
            {
                userRolesTable.DeleteOnSubmit(userRoles);
                context.SubmitChanges();
                return true;
            }
            return false;
        }

        public void DeleteUserFromRole(string userName, string roleName, string appName)
        {
            UserRoles userRoles = userRolesTable.SingleOrDefault(u => u.UserName == userName && u.RoleName == roleName && u.AppName == appName);
            if (userRoles != null)
            {
                userRolesTable.DeleteOnSubmit(userRoles);
                context.SubmitChanges();
            }
        }
    }
}